package com.bookshop.scheduler;

import com.bookshop.configuration.BloomFilterManager;
import com.bookshop.entity.Order;
import com.bookshop.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author g2l
 * @create 2022-09-16 22:29
 */
@Slf4j
@Component
public class OrderJob implements Job {
    @Resource
    private Scheduler scheduler;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 执行任务的方法，Quartz框架会在触发器触发时调用此方法。
     *
     * @param jobExecutionContext 包含任务执行的上下文信息，提供了访问任务数据等功能
     * @throws JobExecutionException 如果任务执行过程中发生异常，可以抛出此异常
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String orderNo = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("orderNo");

        JobKey jobKey = new JobKey(orderNo , "InitPayStatusQuartz");
        Order order = orderMapper.searchByOrderNo(orderNo);

       if(order.getPayStatus() == 0)
       {
           orderMapper.updateOutTime(orderNo , 2);
       }

        try {
            if (scheduler.checkExists(jobKey)) {
                scheduler.deleteJob(jobKey);

            }
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }

    }

}
